#include <iostream>
#include <string>
#include <vector>
#include "friend.pb.h"
#include "mprpcapplication.h"
#include "mprpcprovider.h"

class FriendService : public fixbug::FriendServiceRpc
{
public:
    std::vector<std::string> GetFriendsList(uint32_t userid)
    {
        std::cout << "do GetFriendList service! userid: " << userid << std::endl;
        std::vector<std::string> vec;  
        vec.push_back("wangzn2016");
        vec.push_back("wangzn2002");
        vec.push_back("wangzn2024");
        return vec;
    }

    /*
    message GetFriendsListRequest
    {
        uint32 userid = 1;
    }
    message GetFriendsListResponse
    {
        ResultCode result = 1;
        repeated bytes friends = 2;
    }
    */
    // 重写基类方法
    void GetFriendsList(::google::protobuf::RpcController* controller,
                       const ::fixbug::GetFriendsListRequest* request,
                       ::fixbug::GetFriendsListResponse* response,
                       ::google::protobuf::Closure* done)
    {
        // 获取userid
        uint32_t userid = request->userid();
        // 调用本地GetFriendsList()方法
        std::vector<std::string> friendsList = GetFriendsList(userid);
        response->mutable_result()->set_errcode(0);
        response->mutable_result()->set_errmsg("");
        for (auto& name : friendsList) {
            std::string *pfriends = response->add_friends();
            *pfriends = name;
        }

        done->Run();
    }
};

int main(int argc, char** argv)
{
    // 调用框架的初始化操作
    MprpcApplication::Init(argc, argv);

    RpcProvider provider;
    provider.NotifyService(new FriendService());

    provider.Run();

    return 0;
}